﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using CacheRedis.Models;
using CacheCore.Caching;
using Microsoft.Extensions.Caching.Memory;
using EasyCaching.Core;

namespace CacheRedis.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IStaticCacheManager _staticCacheManager;
        private MemoryCacheManager _memoryStaticCacheManager;
        private RedisCacheManager _redisStaticCacheManager;
        private readonly IEasyCachingProvider _easyCaching;

        public HomeController(ILogger<HomeController> logger, IStaticCacheManager staticCacheManager, IEasyCachingProvider easyCaching)
        {
            _logger = logger;
            _staticCacheManager = staticCacheManager;
            _memoryStaticCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions()));
            _easyCaching = easyCaching;
        }

        public IActionResult Index()
        {
            // var cacheKey = _cacheKeyService.PrepareKeyForDefaultCache(NopBlogsDefaults.BlogTagsModelCacheKey, languageId, storeId, showHidden);
            //_staticCacheManager.Set(new CacheKey("some_key_1"), 3);
            //var a = _staticCacheManager.Get(new CacheKey("some_key_1"), () => 0);
            //_memoryStaticCacheManager.Set(new CacheKey("some_key_2"), 5);
            //var b = _memoryStaticCacheManager.Get(new CacheKey("some_key_2"), () => 0);

            _easyCaching.TrySet<string>("some_key_3", "this is key_3 value", TimeSpan.FromMinutes(2));
            _easyCaching.Set("some_key_4", 7, TimeSpan.FromMinutes(2));
            var c = _easyCaching.Get<string>("some_key_3");
            var d = _easyCaching.Get<int>("some_key_4");
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
